const PriorityQueue = require("../template")

function testPriorityQueueBasic() {
  const pq = new PriorityQueue()

  console.log(
    "1. 基本插入测试:",
    (() => {
      pq.enqueue(10)
      pq.enqueue(5)
      pq.enqueue(15)
      pq.enqueue(3)
      pq.enqueue(8)
      return pq.size() === 5 &&
             pq.isEmpty() === false
    })()
  )

  console.log(
    "2. 基本删除测试:",
    (() => {
      const min1 = pq.dequeue()
      const min2 = pq.dequeue()
      const min3 = pq.dequeue()
      return min1 === 3 &&
             min2 === 5 &&
             min3 === 8 &&
             pq.size() === 2
    })()
  )

  console.log(
    "3. 查看最高优先级元素测试:",
    (() => {
      const peek1 = pq.peek()
      const min = pq.dequeue()
      const peek2 = pq.peek()
      return peek1 === 10 &&
             min === 10 &&
             peek2 === 15
    })()
  )

  console.log(
    "4. 优先队列性质测试:",
    (() => {
      return pq.isValid() === true &&
             pq.size() === 1 &&
             pq.peek() === 15
    })()
  )

  console.log(
    "5. 自定义比较函数测试:",
    (() => {
      const maxPq = new PriorityQueue((a, b) => b - a) // 最大堆
      maxPq.enqueue(10)
      maxPq.enqueue(5)
      maxPq.enqueue(15)
      maxPq.enqueue(3)
      const max = maxPq.dequeue()
      return max === 15 &&
             maxPq.peek() === 10
    })()
  )

  console.log(
    "6. 获取所有元素测试:",
    (() => {
      const elements = pq.toArray()
      return elements.length === 1 &&
             elements[0] === 15
    })()
  )

  console.log(
    "7. this上下文优先队列测试:",
    (() => {
      const pqObj = {
        multiplier: 2,
        processPriorityQueue: function(values) {
          const pq = new PriorityQueue()
          values.forEach(val => pq.enqueue(val))
          return pq.size() * this.multiplier
        }
      }
      return pqObj.processPriorityQueue([1, 2, 3]) === 6 // 3*2=6
    })()
  )

  console.log(
    "8. 复杂对象this绑定优先队列测试:",
    (() => {
      const pqConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(values) {
          const pq = new PriorityQueue()
          values.forEach(val => pq.enqueue(val))
          return pq.size() >= this.config.threshold
        }
      }
      return pqConfigObj.processWithConfig([1, 2, 3]) === true
    })()
  )

  console.log(
    "9. 优先队列一致性测试:",
    (() => {
      const consistencyPq = new PriorityQueue()
      consistencyPq.enqueue(5)
      consistencyPq.enqueue(3)
      consistencyPq.enqueue(7)
      const size1 = consistencyPq.size()
      const min = consistencyPq.dequeue()
      const size2 = consistencyPq.size()
      return size1 === 3 && min === 3 && size2 === 2
    })()
  )

  console.log(
    "10. 优先队列边界测试:",
    (() => {
      const boundaryPq = new PriorityQueue()
      boundaryPq.enqueue(1)
      return boundaryPq.peek() === 1 &&
             boundaryPq.size() === 1 &&
             boundaryPq.isEmpty() === false
    })()
  )

  console.log(
    "11. 优先队列性能测试:",
    (() => {
      const perfPq = new PriorityQueue()
      const values = Array.from({ length: 100 }, (_, i) => i + 1)
      const start = performance.now()
      values.forEach(val => perfPq.enqueue(val))
      for (let i = 0; i < 100; i++) {
        perfPq.dequeue()
      }
      const end = performance.now()
      return perfPq.size() === 0 && (end - start) < 1000
    })()
  )

  console.log(
    "12. 优先队列复杂测试:",
    (() => {
      const complexPq = new PriorityQueue()
      const values = [50, 30, 70, 20, 40, 60, 80, 10, 25, 35]
      values.forEach(val => complexPq.enqueue(val))
      const isValid = complexPq.isValid()
      const size = complexPq.size()
      const min = complexPq.peek()
      return isValid === true &&
             size === 10 &&
             min === 10
    })()
  )
}

testPriorityQueueBasic()
